package coursecase;

import java.io.File;
import java.util.*;

// 寻找目标文件，找到后询问是否删除
public class Test1 {
    public static void main(String[] args) {
        Test1 test1 = new Test1();
        List<File> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要扫描的目录:");
        String path = scanner.nextLine();
        File filepath = new File(path);
        System.out.print("请输入关键字:");
        String key = scanner.nextLine();
        test1.search(key,filepath,list);
        for (File file : list) {
            System.out.println(file.getAbsolutePath());
            System.out.print("请问该文件是否要删除 y/n:");
            if(scanner.nextLine().equals("y")) {
                file.delete();
                System.out.println("文件已C:\\CSDN删除");
            }
        }
    }
    public void search(String key,File path,List<File> list) {
        File[] files = path.listFiles();
        if(files == null || files.length == 0) return;
        for(File file : files) {
            if (file.isDirectory()) {
                search(key,file,list);
            }else {
                if (file.getName().contains(key)) {
                    list.add(file);
                }
            }
        }
    }
}
